#include "TerminalPlugin.h"

#include <QtCore>
#include <QAction>
#include <QWidget>

#include <juffed/Log.h>
//#include <juffed/MainSettings.h>
//#include <juffed/PluginSettings.h>

#include "qtermwidget.h"

TerminalPlugin::TerminalPlugin() : QObject(), JuffPlugin() {
//	w_ = new QWidget();
	w_ = new QTermWidget();
	w_->setWindowTitle(tr("Terminal"));
	
	termAct_ = new QAction(tr("Show/Hide terminal"), 0);
	termAct_->setShortcut(QKeySequence("Ctrl+Alt+T"));
	connect(termAct_, SIGNAL(triggered()), SLOT(showTerminal()));
}

TerminalPlugin::~TerminalPlugin() {
	delete w_;
}

QString TerminalPlugin::name() const {
	return "Terminal";
}

QString TerminalPlugin::targetEngine() const {
	return "all";
}

QString TerminalPlugin::description() const {
	return "Terminal emulator";
}

QWidgetList TerminalPlugin::dockList() const {
	QWidgetList wList;
	wList << w_;
	return wList;
}

Juff::ActionList TerminalPlugin::mainMenuActions(Juff::MenuID id) const
{
	Juff::ActionList list;
	if ( Juff::ID_MENU_TOOLS == id ) {
		list << termAct_;
	}
	return list;
}



void TerminalPlugin::showTerminal()
{
	QWidget* dock = w_->parentWidget();
	if ( NULL == dock )
		return;
	
	if ( dock->isHidden() ) {
		dock->show();
		w_->setFocus();
	}
	else {
		dock->hide();
		QString curDocName;
		manager()->getCurDocName(curDocName);
		if ( !curDocName.isEmpty() )
			manager()->openDoc(curDocName);
	}
}

Q_EXPORT_PLUGIN2(terminal, TerminalPlugin)
